# -*- coding: utf-8 -*-
####################################################
# library import and object instance.              #
####################################################
# import
import os
import sys
from sqlite3 import dbapi2 as sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
sys.path.append('D:\\_workspace\\_python_workspace\\nlib')
from Nlog import Nlog
import MySQLdb as mdb


####################################################
# application configration                         #
####################################################
app = Flask(__name__)
DATABASE = 'D:\_workspace\_python_workspace\flaskCRUDExample\flask_crud.db'
app.config.update(dict(
	DEBUG=True,
	SECRET_KEY='development key',
	USERNAME='admin',
	PASSWORD='default'
))

app.config.from_envvar('FLASKR_SETTINGS', silent=True)
db = mdb.connect("localhost","root","ayaxyz1425gg","python", charset='utf8')
cur = db.cursor(mdb.cursors.DictCursor)
nlog = Nlog();
nlog.puts('test');

####################################################
# method                                           #
####################################################

@app.before_request
def before_request():
	print 'before_request'

####################################################
# route                                            #
####################################################
@app.route('/')
def list():
	cur.execute('SELECT id, title, contents FROM entries ORDER BY id DESC')
	entries = cur.fetchall()
	template = render_template('list.html', entries=entries)
	return template

@app.route('/create')
def create():
	template = render_template('create.html')
	return template

@app.route('/create', methods=['POST'])
def create_proc():
	cur.execute('INSERT INTO entries (title, contents) VALUES (%s, %s)', [request.form['title'], request.form['contents']])
	db.commit();
	flash('New entry was successfully posted')
	template = redirect(url_for('list'))
	return template

@app.route('/retrieve/<int:id>')
def retrieve(id):
	cur.execute('SELECT * FROM entries WHERE id="%s"' % id)
	entry = cur.fetchone()
	template = render_template('retrieve.html', entry=entry, id=id)
	return template

@app.route('/update/<int:id>')
def update(id):
	cur.execute('SELECT * FROM entries WHERE id="%s"' % id)
	entry = cur.fetchone()
	template = render_template('update.html', entry=entry, id=id)
	return template

@app.route('/update', methods=['POST'])
def update_proc():
	cur.execute('UPDATE entries SET title=(%s), contents=(%s) WHERE id=(%s)', [request.form['title'], request.form['contents'], request.form['id']])
	template = redirect(url_for('retrieve', id=request.form['id']))
	return template

@app.route('/delete/<int:id>')
def delete(id):
	cur.execute('DELETE FROM entries WHERE id="%s"' % id)
	db.commit()
	template = render_template('delete.html', id=id)
	return template

####################################################
# and here we go                                    #
####################################################\
if __name__ == '__main__':
	app.run(debug=True)